package com.allen.study.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allen.study.application.api.request.EmployeeInfoQueryRequest;
import com.allen.study.application.api.response.EmployeeInfoQueryResponse;
import com.allen.study.application.repository.IEmployeeInfoReadModelRepo;
import com.allen.study.domain.entity.EmployeeInfo;
import com.allen.study.domain.repository.IEmployeeInfoRepo;
import com.allen.study.common.base.Pagination;
import com.allen.study.infrastructure.persistence.assembler.EmployeeInfoPOAssembler;
import com.allen.study.infrastructure.persistence.dao.IEmployeeInfoMapper;
import com.allen.study.infrastructure.persistence.po.EmployeeInfoPO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 用户信息表
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class EmployeeInfoRepoImpl extends ServiceImpl<IEmployeeInfoMapper, EmployeeInfoPO>
        implements IEmployeeInfoRepo, IEmployeeInfoReadModelRepo {

    /**
     * 用户信息表持久化对象结构映射
     */
    private final EmployeeInfoPOAssembler employeeInfoPOAssembler;

    @Override
    public List<EmployeeInfoQueryResponse> query(Pagination pagination, EmployeeInfoQueryRequest queryRequest) {
        // 构造查询条件
        LambdaQueryWrapper<EmployeeInfoPO> lambdaQueryWrapper = Wrappers.lambdaQuery();


        // 如果 员工工号 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEmployeeId())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getEmployeeId, queryRequest.getEmployeeId());
        }
        // 如果 员工姓名 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEmployeeName())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getEmployeeName, queryRequest.getEmployeeName());
        }
        // 如果 年龄 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getAge())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getAge, queryRequest.getAge());
        }
        // 如果 性别：0-男，1-女 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getSex())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getSex, queryRequest.getSex());
        }
        // 如果 组长工号 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getLeaderId())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getLeaderId, queryRequest.getLeaderId());
        }
        // 如果 项目经理工号 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getPmId())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getPmId, queryRequest.getPmId());
        }
        // 如果 主管工号 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getSupervisorId())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getSupervisorId, queryRequest.getSupervisorId());
        }
        // 如果 生日 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getBirthday())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getBirthday, queryRequest.getBirthday());
        }
        // 如果 学历：0-初中及以下，1-高中，2-大专，3-本科，4-硕士，5-博士 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEducation())) {
            lambdaQueryWrapper.eq(EmployeeInfoPO::getEducation, queryRequest.getEducation());
        }

        // 默认按照主键倒序排序
        lambdaQueryWrapper.orderByDesc(EmployeeInfoPO::getId);

        // 分页查询
        Page<EmployeeInfoPO> page = super.page(Page.of(pagination.getPageNumber(), pagination.getPageSize()), lambdaQueryWrapper);
        pagination.setTotal(page.getTotal());
        // 用户信息表持久化对象 转 用户信息表查询响应对象
        return employeeInfoPOAssembler.toQueryResponse(page.getRecords());
    }

    @Override
    public EmployeeInfo queryById(String employeeInfoId) {
        // 根据主键查询用户信息表
        EmployeeInfoPO employeeInfoPO = super.getById(employeeInfoId);
        // 用户信息表持久化对象 转 用户信息表响应对象
        return employeeInfoPOAssembler.toEntity(employeeInfoPO);
    }

    @Override
    public List<EmployeeInfo> queryByIds(List<String> employeeInfoIds) {
        if(ObjectUtils.isEmpty(employeeInfoIds)){
            return Collections.emptyList();
        }
        return employeeInfoPOAssembler.toEntity(super.listByIds(employeeInfoIds));
    }

    @Override
    public List<EmployeeInfo> queryByEmpIds(List<String> empIds) {
        if(ObjectUtils.isEmpty(empIds)){
            return Collections.emptyList();
        }
        LambdaQueryWrapper<EmployeeInfoPO> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EmployeeInfoPO::getEmployeeId, empIds);
        List<EmployeeInfoPO> list = super.list(lambdaQueryWrapper);
        return employeeInfoPOAssembler.toEntity(list);
    }

    @Override
    public void create(EmployeeInfo employeeInfo) {
        // 用户信息表实体转用户信息表持久化对象
        EmployeeInfoPO employeeInfoPO = employeeInfoPOAssembler.assembler(employeeInfo);

        // 创建用户信息表
        super.save(employeeInfoPO);
    }

    @Override
    public void deleteById(String employeeInfoId) {
        // 物理删除用户信息表
        super.removeById(employeeInfoId);
    }

    @Override
    public void updateById(EmployeeInfo employeeInfo) {
        // 用户信息表实体转用户信息表持久化对象
        EmployeeInfoPO employeeInfoPO = employeeInfoPOAssembler.assembler(employeeInfo);
        // 更新用户信息表
        super.updateById(employeeInfoPO);
    }
}
